package com.server.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.entity.UpmsDict;
import com.server.entity.UpmsOrganization;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import com.server.util.Result;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import com.server.entity.UpmsRole;
import com.server.service.IUpmsRoleService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.server.util.BaseController;

/**
 * <p>
 * 角色 控制器
 * </p>
 *
 * @author
 * @since 2020-04-03
 */
@RestController
@Controller
@AllArgsConstructor
@Slf4j
@Api(value = "角色管理", tags = "角色管理")
@RequestMapping("/admin/role")
public class AdminUpmsRoleController extends BaseController {

	private IUpmsRoleService upmsRoleService;



    /**
     * 根据id查询详情
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findById")
	@ApiOperation(value = "根据id查询详情")
    @ResponseBody
    public Result<UpmsRole> findById(@ApiParam(name = "id", value = "角色id", required = true) @RequestParam String id) {
        return Result.returnData(upmsRoleService.findById(id));
    }

	 /**
     * 新增
     *
     * @author
     * @Date 2020-04-03
     */
	@PostMapping("/add")
    @ApiOperation(value = "新增")
    @ResponseBody
    public Result add(@ApiParam(name = "entity", value = "新增对象")@Valid @RequestBody UpmsRole entity) {
        if(upmsRoleService.count(new QueryWrapper<UpmsRole>().lambda().eq(UpmsRole::getName,entity.getName())
        )>0){
            return Result.error("名称不能重复");
        }
        return upmsRoleService.add(entity);
    }

	/**
     * 修改
     *
     * @author
     * @Date 2020-04-03
     */
	@PostMapping("/update")
    @ApiOperation(value = "修改")
    @ResponseBody
    public Result update(@ApiParam(name = "entity", value = "修改对象")@Valid @RequestBody UpmsRole entity) {
        if(entity.getRoleId()==null){
            return Result.error("id不能为空");
        }
        if(upmsRoleService.count(new QueryWrapper<UpmsRole>().lambda().eq(UpmsRole::getName,entity.getName())
                .ne(UpmsRole::getRoleId,entity.getRoleId()))>0){
            return Result.error("组织名称不能重复");
        }

        UpmsRole old =  upmsRoleService.getById(entity.getRoleId());
        if(old==null){
            return Result.error("找不到对应记录");
        }
        BeanUtil.copyProperties(entity,old);
        return  upmsRoleService.update(old);
    }

    /**
     * 批量删除
     *
     * @author
     * @Date 2020-04-03
     */
    @DeleteMapping("/del")
	@ApiOperation(value = "批量删除")
    @ResponseBody
    public Result delete(@ApiParam(name = "ids", value = "批量删除", required = true)@Valid @RequestParam String ids) {
        return upmsRoleService.delete(ids)?Result.ok("操作成功"):Result.error("操作失败");
    }

}
